
#include "cbase.h"
#include "hud.h"
#include "c_hudflag.h"
#include "iclientmode.h"
#include "hud_macros.h"
#include "vgui_controls/controls.h"
#include "vgui/ISurface.h"

#include <stdlib.h>
#include <string.h>

#include "tier0/memdbgon.h" 

using namespace vgui;


//------------------------------------
//HUD CLASS
//------------------------------------

DECLARE_HUDELEMENT( C_flagsystem );
DECLARE_HUD_MESSAGE( C_flagsystem, FlagInput2 );


C_flagsystem::C_flagsystem(const char *pElementName) : CHudElement( pElementName ), BaseClass( NULL, "FlagPanel" )
{
		
}

/*C_flagsystem::~C_flagsystem() //edited by Gus
{
}
*/

void C_flagsystem::Init()
{
	HOOK_HUD_MESSAGE( C_flagsystem, FlagInput2 );
	
	Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );  
	 
	SetVisible(true); 
	SetPaintBorderEnabled(true);  
	SetAlpha( 4 );

	//SetVisible(true); 
	//SetPaintEnabled(true);
	//SetPaintBackgroundEnabled(true);

   		//SetBounds( 0,0, 256, 256 );
		//SetBgColor( Color( 60,60,60,100 ) );		
}

void C_flagsystem::VidInit( void )
{
}


void C_flagsystem::MsgFunc_FlagInput2( bf_read &msg )
{
	int index;
	int m_iFlag;
	m_iFlag = msg.ReadByte(); 
	index = msg.ReadByte();
	m_iFlagste[index]=m_iFlag; // Get flag state inputs from server
	msg.ReadString( m_sPath_ol, sizeof(m_sPath_ol) );
	m_nMaxflags_ol =  msg.ReadByte();
	 
	//SetHiddenBits( HIDEHUD_PLAYERDEAD ); 

	// MAKES PATH TO FLAGS


	GetSize( wide, tall ); 
	wide=wide/1.3;

	if ( m_nMaxflags_ol>0 )
	{ 

		char m_sPath[256];
		char m_sPath1[256];
		strcpy( m_sPath1, m_sPath_ol );
		strcpy ( m_sPath, "" );
		
		char extra[20]="hud/flags/";
		char extra1[20]="/flag";

		strcat ( m_sPath, extra );

		strcat( m_sPath, m_sPath1 );

		strcat ( m_sPath, extra1 );


			char a[2]="a";
			char b[2]="b";
			char c[2]="c";
	 
	   for (int i=0; i<m_nMaxflags_ol; i++) { //AW Create Texture for Looking around
			
		   if ( m_nTextureID[i]==0 || m_nTextureID[i]==false)
		   { 		  
				m_nTextureID[i] = surface()->CreateNewTextureID();
		   }
	

			//creates path to textures

			char Path[256]="";
			strcat ( Path, m_sPath );

			char ii[5];


			itoa (i,ii,10); // convert int to char
			
			if (m_iFlagste[i]==0) { 
				
				strcat ( Path, ii );
				strcat ( Path, a );
			}
											
			if (m_iFlagste[i]==1) { 

				strcat ( Path, ii );
				strcat ( Path, b );
				}

			if (m_iFlagste[i]==2) { 

				strcat ( Path, ii );
				strcat ( Path, c );
				}
			
			//Warning("Final Path is:%s\n", Path);
		
			surface()->DrawSetTextureFile( m_nTextureID[i], Path, true, true);
	   }
		
	}




}



void C_flagsystem::OnThink()
{
   BaseClass::OnThink();
   
	//Paint();
}


void C_flagsystem::Paint()
{
	SetPaintBorderEnabled(false);  
       
	int x=0, y=0, x1=wide, y1=wide;
	for (int f=0; f<m_nMaxflags_ol; f++) {
		surface()->DrawSetTexture( m_nTextureID[f] );
		surface()->DrawTexturedRect( x, y, x1, y1 );
		//x=x+0;
		//x1=x1+0;
		y1=y1+wide+4;
		y=y+wide+4;
	}

}